package com.kejizhentan.controller.api;

import com.kejizhentan.common.exception.YyghException;
import com.kejizhentan.common.result.Result;
import com.kejizhentan.common.result.ResultCodeEnum;
import com.kejizhentan.common.utils.MD5;
import com.kejizhentan.helper.HttpRequestHelper;
import com.kejizhentan.service.DepartmentService;
import com.kejizhentan.service.HospitalService;
import com.kejizhentan.service.HospitalSetService;
import com.kejizhentan.service.ScheduleService;
import com.kejizhentan.yygh.model.hosp.Department;
import com.kejizhentan.yygh.model.hosp.Hospital;
import com.kejizhentan.yygh.model.hosp.Schedule;
import com.kejizhentan.yygh.vo.hosp.DepartmentQueryVo;
import com.kejizhentan.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author shikaifeng
 * @Date 2022/10/1 13:14
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/hosp/")
public class HospitalApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    // 上传排版接口
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        // 获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        // TODO 签名校验
        scheduleService.save(paramMap);
        return Result.ok();
    }

    // 查询排班
    @PostMapping("/schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        // 获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        // 获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        // 获取科室编号
        String depcode = (String) paramMap.get("depcode");
        // 当前页
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        // 每页显示记录数
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));
        // TODO 签名校验
        ScheduleQueryVo scheduleQueryVo = ScheduleQueryVo
                .builder()
                .hoscode(hoscode)
                .depcode(depcode)
                .build();
        // 调用 service
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page, limit, scheduleQueryVo);
        return Result.ok(pageModel);
    }

    // 删除排班
    @PostMapping("/schedule/remove")
    public Result remove(HttpServletRequest request) {
        // 获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        // 获取医院编号和排班编号
        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");

        // TODO 签名校验

        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }

    // 删除科室
    @PostMapping("/department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        // 获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        // 获取医院编号和科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        // TODO 签名校验
//        // 获取医院系统传递过来的签名，签名进行了 MD5 加密
//        String hospSign = (String) paramMap.get("sign");
//        String signKey = hospitalSetService.getSignKey(hoscode);
//        // 把签名进行 MD5 加密
//        String signKeyMd5 = MD5.encrypt(signKey);
//        // 判断签名是否一直
//        if (!hospSign.equals(signKeyMd5)) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }
        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }

    // 查询科室
    @PostMapping("department/list")
    public Result getDepartment(HttpServletRequest request) {
        // 获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        // 获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        // 当前页
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        // 每页显示记录数
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));
        // TODO 签名校验
//        // 获取医院系统传递过来的签名，签名进行了 MD5 加密
//        String hospSign = (String) paramMap.get("sign");
//        String signKey = hospitalSetService.getSignKey(hoscode);
//        // 把签名进行 MD5 加密
//        String signKeyMd5 = MD5.encrypt(signKey);
//        // 判断签名是否一直
//        if (!hospSign.equals(signKeyMd5)) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }

        DepartmentQueryVo departmentQueryVo = DepartmentQueryVo.builder()
                .hoscode(hoscode)
                .build();
        // 调用 service
        Page<Department> pageModel = departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(pageModel);
    }

    // 上传科室
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        // 获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        // 获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        // 获取医院系统传递过来的签名，签名进行了 MD5 加密
//        String hospSign = (String) paramMap.get("sign");
//        String signKey = hospitalSetService.getSignKey(hoscode);
//        String signKeyMd5 = MD5.encrypt(signKey);
//        // 判断签名是否一直
//        if (!hospSign.equals(signKeyMd5)) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }
        // 调用 service
        departmentService.save(paramMap);
        return Result.ok();
    }

    // 查询医院
    @PostMapping("/hospital/show")
    public Result getHospital(HttpServletRequest request) {
        // 获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        // 获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        // 获取医院系统传递过来的签名，签名进行了 MD5 加密
//        String hospSign = (String) paramMap.get("sign");
//        String signKey = hospitalSetService.getSignKey(hoscode);
//        // 把签名进行 MD5 加密
//        String signKeyMd5 = MD5.encrypt(signKey);
//        // 判断签名是否一直
//        if (!hospSign.equals(signKeyMd5)) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }

        // 调用 service
        Hospital hospital = hospitalService.getByHosCode(hoscode);

        return Result.ok(hospital);
    }

    //上传医院接口
    @PostMapping("/saveHospital")
    public Result saveHosp(HttpServletRequest request){
        // 获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //通过工具类将string->string[]的键值对结构转换成string->object结构
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        // 获取医院系统传递过来的签名，签名进行了 MD5 加密
        String hospSign = (String) paramMap.get("sign");
        // 根据传递过来的医院编号，查询数据库签名
        String hoscode = (String) paramMap.get("hoscode");
//        String signKey = hospitalSetService.getSignKey(hoscode);
//        // 把签名进行 MD5 加密
//        String signKeyMd5 = MD5.encrypt(signKey);
//        // 判断签名是否一直
//        if (!hospSign.equals(signKeyMd5)) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }
        // 传输过程中“+”转换为了“  ”，因此需要转换
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logoData", logoData);

        //调用service方法
        hospitalService.save(paramMap);
        return Result.ok();
    }
}
